#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.views import View
from django.shortcuts import render,HttpResponse,redirect,reverse
from repository import models


class LoginView(View):
    def get(self, request, *args, **kwargs):
        return render(request,"login.html")

    def post(self, request, *args, **kwargs):
        username = request.POST.get("username")
        password = request.POST.get("password")
        admin = models.AdminInfo.objects.filter(username=username,password=password).first()
        if admin:
            request.session["user"] = username
            return render(request,"cmdbindex.html")
        else:
            msg = "用户名或密码错误"
            return render(request,"login.html",{"msg":msg})


class LogoutView(View):
    def get(self, request, *args, **kwargs):
        request.session.delete()
        return redirect("login")